package com.zhongya.example.view

import android.content.Context
import android.graphics.*
import android.os.Build
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.annotation.RequiresApi

class RestoreToCountView : View {

    val mPaint = Paint()

    constructor(context: Context) : super(context) {

    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
//        setLayerType(LAYER_TYPE_SOFTWARE, null)
   }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        val id1 = canvas?.save()
        canvas?.clipRect(0f,0f,800f,800f)
        canvas?.drawColor(Color.RED)
        Log.e("wzy","saveCount:${canvas?.saveCount}--id1:${id1}")

        val id2 = canvas?.saveLayer(0f,0f,width.toFloat(),height.toFloat(),mPaint)
        canvas?.clipRect(100f,100f,700f,700f)
        canvas?.drawColor(Color.GREEN)
        Log.e("wzy","saveCount:${canvas?.saveCount}--id2:${id2}")

        val id3 = canvas?.saveLayerAlpha(0f,0f,width.toFloat(),height.toFloat(),0xff.toInt())
        canvas?.clipRect(200f,200f,600f,600f)
        canvas?.drawColor(Color.YELLOW)
        Log.e("wzy","saveCount:${canvas?.saveCount}--id3:${id3}")

        val id4 = canvas?.save()
        canvas?.clipRect(300f,300f,500f,500f)
        canvas?.drawColor(Color.BLUE)
        Log.e("wzy","saveCount:${canvas?.saveCount}--id4:${id4}")

        canvas?.restoreToCount(id3!!)
//        canvas?.drawColor(Color.GRAY)
        mPaint.color = Color.GRAY
        canvas?.drawRect(50f,50f,300f,300f,mPaint)
        Log.e("wzy","saveCount:${canvas?.saveCount}")

    }
}